{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例一#\n",
    "from tkinter import *    #导入tkinter模块所有内容\n",
    "from tkinter import messagebox   #导入tkinter模块中的子模块messagebox\n",
    "\n",
    "root=Tk()   # 创建1个根窗口组件 root\n",
    "root.geometry(\"280x100\")   # 设置窗口大小\n",
    "btn_Say=Button(root)  # 创建1个按钮组件btn_Say，作为root的子组件\n",
    "btn_Say['text']='Hello'   # 设置 btn_Say 的text属性\n",
    "btn_Say.pack()    # 放置组件 btn_Say\n",
    "def say_good(e):   # 定义单击组件 btn_Say的事件处理程序\n",
    "    messagebox.showinfo(\"Tk示例\",\"早安，培正！\")\n",
    "btn_Say.bind(\"<Button-1>\",say_good)  # 绑定事件处理程序\n",
    "root.mainloop()   # 调用组件的mainloop()方法，进入事件循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例二#\n",
    "import tkinter as tk        # 导入tkinter模块并取别名tk\n",
    "def say_hello():               # 第四步，定义控件的功能代码\n",
    "    print(\"培正，早安！\")\n",
    "\n",
    "if __name__==\"__main__\":\n",
    "    window = tk.Tk()           # 第2步，创建主窗口\n",
    "    window.title(\"示例演示\")        # 设置窗口标题\n",
    "    window.geometry(\"280x100\")     # 设置窗口大小\n",
    "    # 第3步，创建控件，设置属性并关联功能代码\n",
    "    button_hello = tk.Button(window,text=\"培正，早安！\\n(click me)\",command=say_hello)\n",
    "    button_quit = tk.Button(window,text=\"QUIT\", fg=\"red\",command=window.destroy)\n",
    "    #第5步，控件布局，指定控件的位置\n",
    "    button_hello.pack(side='top')\n",
    "    button_quit.pack(side='bottom')\n",
    "    window.mainloop()   # 第6步，进入事件循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例三#\n",
    "from tkinter import *\n",
    "\n",
    "window = Tk()\n",
    "window.geometry('280x150')\n",
    "window.title('pack布局示例')\n",
    "button_one = Button(text='按钮1')\n",
    "button_one.pack(side=LEFT)\n",
    "button_two = Button(text='按钮2')\n",
    "button_two.pack(side=RIGHT)\n",
    "button_three = Button(text='按钮3')\n",
    "button_three.pack(side=TOP)\n",
    "button_four = Button(text='按钮4')\n",
    "button_four.pack(side=BOTTOM)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例四#\n",
    "from tkinter import *\n",
    "\n",
    "window = Tk()\n",
    "window.geometry('500x300')\n",
    "window.title('pack布局示例')\n",
    "#放置10个Lable控件，展示pack放置方式和配置\n",
    "Label(window,text='TOP1,fill=NONE',bg='red').pack(side=TOP,fill=NONE)\n",
    "Label(window,text='TOP2,fill=X',bg='yellow').pack(side=TOP,fill=X)\n",
    "Label(window,text='BOTTOM1,ipadx=0',bg='red').pack(side=BOTTOM,ipadx=0)\n",
    "Label(window,text='BOTTOM2,ipadx=40,ipady=10',bg='yellow').pack(side=BOTTOM,ipadx=40,ipady=10)\n",
    "Label(window,text='LEFT1\\nexpand=False',bg='light blue').pack(side=LEFT,expand=False)\n",
    "Label(window,text='LEFT2\\nexpand=False',bg='yellow2').pack(side=LEFT,expand=False)\n",
    "Label(window,text='LEFT3\\nexpand=True',bg='light green').pack(side=LEFT,expand=True)\n",
    "Label(window,text='RIGHT1\\npadx=10\\npady=0\\nfill=NONE',bg='pink').pack(side=RIGHT,padx=10)\n",
    "Label(window,text='RIGHT2\\npadx=0\\npady=10\\nfill=tk.Y',bg='skyblue1').pack(side=RIGHT,padx=0,pady=10,fill=Y)\n",
    "Label(window,text='RIGHT2\\npadx=0\\npady=0\\nfill=tk.Y',bg='coral1').pack(side=RIGHT,padx=0,fill=Y)\n",
    "window.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加法运算器#\n",
    "from tkinter import *\n",
    "\n",
    "window = Tk()\n",
    "window.geometry('280x150')\n",
    "window.title('grid布局示例1')\n",
    "Label(window, text=\"第一个数\").grid(row=0)\n",
    "Label(window, text=\"第二个数\").grid(row=1)\n",
    "Label(window, text=\"求和结果\").grid(row=2)\n",
    "entry_one = Entry(window)\n",
    "entry_two = Entry(window)\n",
    "entry_three = Entry(window)\n",
    "entry_one.grid(row=0, column=1)\n",
    "entry_two.grid(row=1, column=1)\n",
    "entry_three.grid(row=2, column=1)\n",
    "def sum1():\n",
    "    result = float(entry_one.get()) + float(entry_two.get())\n",
    "    entry_three.insert(0,str(result))\n",
    "button = Button(window, text='求和', height=2,command=sum1)\n",
    "button.grid(row=0, column=2, rowspan=2)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例五#\n",
    "from tkinter import *\n",
    "\n",
    "window = Tk()\n",
    "window.geometry('280x150')\n",
    "window.title('place布局示例1')\n",
    "def hello_call_back():\n",
    "      print(\"Hello Python\", \"Hello World\")\n",
    "\n",
    "button = Button(window, text=\"Hello\", command=hello_call_back)\n",
    "button.pack()\n",
    "button.place(relx=0.5, rely=0.5, anchor=CENTER)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例六#\n",
    "from tkinter import *\n",
    "\n",
    "window = Tk()\n",
    "window.geometry('280x150')\n",
    "window.title('place布局示例1')\n",
    "def hello_call_back():\n",
    "    print(\"Hello Python\", \"Hello World\")\n",
    "\n",
    "button = Button(window, text=\"Hello\", command=hello_call_back)\n",
    "button.pack()\n",
    "button.place(relx=0.5, rely=0.5, anchor=CENTER)\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#登录GUI程序#\n",
    "import tkinter as tk\n",
    "from tkinter import messagebox\n",
    "\n",
    "# 登录窗口\n",
    "window = tk.Tk()\n",
    "window.title('学生考试系统')\n",
    "window.geometry('800x500')\n",
    "\n",
    "# 用户名密码标签\n",
    "tk.Label(window, text='用户名:', bg='yellow').place(x=300, y=200)\n",
    "tk.Label(window, text='密   码:', bg='yellow').place(x=300, y=250)\n",
    "\n",
    "# 用户名输入框\n",
    "name = tk.Entry(window,bd=3,width=30)\n",
    "name.place(x=370, y=200)\n",
    "# 密码输入框\n",
    "passwd = tk.Entry(window, show='*',bd=3,width=30)\n",
    "passwd.place(x=370, y=250)\n",
    "\n",
    "# 定义登录方法\n",
    "def user_login():\n",
    "    if name.get() == 'admin' and passwd.get() == '123456':\n",
    "        # 定义消息提示框\n",
    "        messagebox.showinfo(title='成功',message='登录成功！')\n",
    "    else:\n",
    "        messagebox.showerror(title='错误',message='登录失败，用户名或密码错误！')\n",
    "# 登录按钮\n",
    "bt_login = tk.Button(window, bg='yellow', text='登录', command=user_login)\n",
    "bt_login.place(x=400, y=300)\n",
    "\"\"\"\n",
    "# 添加退出按钮\n",
    "button_quit = tk.Button(window,text=\"QUIT\", fg=\"red\",command=window.destroy)\n",
    "button_quit.place(x=700,y=450)\n",
    "\"\"\"\n",
    "# 主循环\n",
    "window.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#菜单设计二：下拉菜单设计【垂直菜单设计】#\n",
    "\n",
    "from tkinter import *\n",
    "\n",
    "def sum1():\n",
    "    window = Tk()\n",
    "    window.geometry('280x150')\n",
    "    window.title('grid布局示例1')\n",
    "    Label(window, text=\"第一个数\").grid(row=0)\n",
    "    Label(window, text=\"第二个数\").grid(row=1)\n",
    "    Label(window, text=\"求和结果\").grid(row=2)\n",
    "    entry_one = Entry(window)\n",
    "    entry_two = Entry(window)\n",
    "    entry_three = Entry(window)\n",
    "    entry_one.grid(row=0, column=1)\n",
    "    entry_two.grid(row=1, column=1)\n",
    "    entry_three.grid(row=2, column=1)\n",
    "    def sum2():\n",
    "        result = float(entry_one.get()) + float(entry_two.get())\n",
    "        entry_three.insert(0, str(result))\n",
    "    button = Button(window, text='求和', height=2, command=sum2)\n",
    "    button.grid(row=0, column=2, rowspan=2)\n",
    "    window.mainloop()\n",
    "\n",
    "root = Tk()\n",
    "root.title('菜单设计示例')  # 设置窗体的名称\n",
    "root.geometry('300x300')  # 设置窗体的大小\n",
    "menu_bar = Menu(root)   # 父容器（menu）为根窗体的实例\n",
    "file_menu = Menu(menu_bar)\n",
    "file_menu.add_command(label=\"求和\",command=sum1)\n",
    "file_menu.add_command(label=\"求差\")\n",
    "file_menu.add_command(label=\"求积\")\n",
    "file_menu.add_separator()  #加分隔线\n",
    "file_menu.add_command(label=\"退出\", command=root.quit , accelerator='Ctrl+Alt')\n",
    "# 将菜单项添加到菜单栏\n",
    "menu_bar.add_cascade(label=\"文件\", menu=file_menu)   #不需要先创建横向文件菜单\n",
    "menu_bar.add_command(label=\"编辑\")   #添加顶端（横向）菜单 \"编辑\"\n",
    "menu_bar.add_command(label=\"设计\")   #添加顶端（横向）菜单 \"设计\"\n",
    "menu_bar.add_command(label=\"帮助\")   #添加顶端（横向）菜单 \"帮助\"\n",
    "# 将菜单栏添加到主窗口\n",
    "root['menu'] = menu_bar\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
